package com.scenery.server.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.scenery.server.services.SceneryServices;
import com.scenery.server.vo.Scenery;

/**
 * Servlet implementation class SceneryServlet
 */
public class SceneryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SceneryServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/plain;charset=UTF-8");  
		response.setHeader("Cache-Control","no-cache"); 
		SceneryServices server=new SceneryServices();
		String cityId=request.getParameter("cityId");
		
		PrintWriter out=response.getWriter();
		JSONObject jo=new JSONObject();
		if(cityId==null)
		{
			
			try {
				jo.put("code", "101");
				jo.put("msg", "城市编号不能为空！");
				jo.put("list", "");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else 
		{
			
			List<Scenery> list=new ArrayList<Scenery>();
			list=server.list(cityId);
			
			try {
				jo.put("code", "100");
				jo.put("msg", "成功");
				jo.put("list", list);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}
		out.println(jo.toString());
		out.close();
	}

}
